/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.render.exportable;

import org.apache.commons.lang.StringUtils;
import org.retabel.service.render.LRUCache;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

/**
 * Classloader to load report resources from defined urls.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 23:51 29.06.11
 *
 * @author Burt Parkers
 * @version 1.0
 */
class JasperReportsReportDesignClassLoader extends ClassLoader {

    /** The cache to use. */
    private final LRUCache<String, URI> cache;

    /** Report package name to initalize the ClassLoader. */
    private final String packageName;

    /** The version. */
    private final String version;

    /** The report archive rest interface uri */
    private final URI reportDesignURI;

    /**
     * Creates a ClassLoader instance.
     *
     * @param cacheSize   the cache size to use.
     * @param packageName the package name to use, part of the uri.
     * @param version     the version, part of uri.
     * @param reportDesignURI     the report design base uri
     */
    public JasperReportsReportDesignClassLoader(int cacheSize, String packageName, String version, URI reportDesignURI) {
        this.packageName = packageName;
        this.version = version;
        this.reportDesignURI = reportDesignURI;
        cache = new LRUCache<>(cacheSize);
    }

    /** {@inheritDoc} */
    public Class findClass(String name) {
        return null;
    }

    /** {@inheritDoc} */
    protected URL findResource(String name) {
        try {
            URI uri = cache.get(name);
            if (uri == null) {
                uri = reportDesignURI.resolve(StringUtils.join(new String[]{packageName, "/", version, "/", name}));
                cache.put(name, uri);
            }
            return uri.toURL();
        } catch (MalformedURLException e) {
            return null;
        }
    }

}
